using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            string str_pwd = File.ReadAllText(@"C:\Windows\RM_APP\sam_pwd.txt");
            string str_date= File.ReadAllText(@"C:\Windows\RM_APP\sam_date.txt");
            string str_nowtime = DateTime.Now.ToString("yyyy-MM-dd");
            label1.Text = str_pwd;
            label2.Text = str_date;
            label3.Text = str_nowtime;
            //计算相隔多少天没修改密码了
            TimeSpan sp = Convert.ToDateTime(str_nowtime).Subtract(Convert.ToDateTime(str_date));
            int days = sp.Days;
            if (days < 90)//不到90天，不提示，直接关闭程序
            {
                System.Environment.Exit(0);
            }
            else if (days <= 93)//90-93天，只是提示
            {

            }
            else//超过93天直接锁定，修改密码为管理者手机号
            {
                string admin_tel = "1234567890";
                runcmd("net user Administrator "+ admin_tel + "");
                System.IO.File.WriteAllText(@"C:\Windows\RM_APP\sam_pwd.txt", string.Empty); //清空pwd txt文本内容
                System.IO.File.WriteAllText(@"C:\Windows\RM_APP\sam_pwd.txt", admin_tel); //重新写入新密码pwd txt文本内容
                label1.Text = "******";
                runcmd("rundll32.exe user32.dll LockWorkStation");
            }
        }

        private void button1_Click(object sender, EventArgs e)//修改登录密码
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请输入新密码！");
            }
            else
            {
                if (textBox1.Text == label1.Text)
                {
                    MessageBox.Show("不能与旧密码相同！");
                }
                else
                {
                    if (textBox1.Text.Length <= 3)
                    {
                        MessageBox.Show("不符合密码复杂性！");
                    }
                    else
                    {
                        //CMD修改密码
                        runcmd("net user Administrator "+ textBox1.Text+ "");
                        System.IO.File.WriteAllText(@"C:\Windows\RM_APP\sam_pwd.txt", string.Empty); //清空pwd txt文本内容
                        System.IO.File.WriteAllText(@"C:\Windows\RM_APP\sam_pwd.txt", textBox1.Text); //重新写入新密码pwd txt文本内容
                        System.IO.File.WriteAllText(@"C:\Windows\RM_APP\sam_date.txt", string.Empty); //清空sam_date文本内容
                        System.IO.File.WriteAllText(@"C:\Windows\RM_APP\sam_date.txt", label3.Text); //重新写入新日期sam_date文本内容
                        MessageBox.Show("修改成功，您可继续使用90天！");
                        System.Environment.Exit(0);
                    }
                }
            }
        }
        private static string runcmd(string command)///111
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.Arguments = "/c " + command;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();
            return p.StandardOutput.ReadToEnd();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            MessageBox.Show("本消息只提示3天，超期后电脑将锁定，请及时修改密码！");
        }
    }
}
